<?php

// 评论 管理类

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
use Session;

class CommentsController extends Controller
{
	/**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */

	// 评论 列表 
	public function index(Request $request)
	{
        // 开始
        // 获取路由
        $route_array = request()->route()->getAction();
        $controller = explode("\\",$route_array['controller']);
        $result = explode("@",$controller[3]);
        // 控制器 	$result[0];
        // 方法 	$result[1];
        $auth_final = '';
        foreach( Session::get('auths') as $key=> $auth_info )
        {
            // 符合条件
            if( $auth_info['controller']==$result[0] && $auth_info['func']==$result[1] )
            {
                $auth_final = true;
                break;
            }else
            {
                $auth_final = false;
            }
        }
        // 权限验证失败
        if( !$auth_final )
        {
            return redirect('/')->with( ['authinfo'=>'没有此项权限'] );
        }
        // 结尾
		// 搜索关键字 没有 默认为空
		$keyword = $request -> input('keyword',"");
		if( empty($keyword) )
		{
			// 为空 说明没有搜索
			// 查询所有评论 每页20条数据
			$comments = DB::table('comments as c') 
						-> select( "c.*",DB::raw("case when c.type=1 then '资讯' when c.type=2 then '文章' when c.type=3 then '图库' end as type ") )
						-> orderBy( 'id','desc' ) 
						-> paginate(20);
		}else
		{
				
			// 查询所有评论 每页20条数据
			$comments = DB::table('comments as c') 
						-> select( "c.*",DB::raw("case when c.type=1 then '资讯' when c.type=2 then '文章'  when c.type=3 then '图库' end as type ") )
						-> where('c.content','like','%'.$keyword.'%' ) 
						-> orderBy( 'id','desc' ) 
						-> paginate(20);
		}
		return view( 'comments.list',[ 'comments'=>$comments,'request'=>$keyword,'title'=>'评论列表' ] );
	}
	
	
	// 批量 删除 评论
	public function delAll( Request $request )
	{
		
		// 包含 评论id
		$data = $request -> except('_token');
		
		// 清除无效值
		unset($data['all'][0]);	
		
		// 1.主表
		$res = DB::table('comments')
				-> whereIn( 'id',$data['all'] )
				-> delete();
		if( $res )
		{
			$this -> show_msg('删除成功');
		}
		$this -> show_msg('删除失败');
		
	
	}
	
	
}
